/*
 * TicketCommandGroup.java
 *
 * Created on October 4, 2007, 11:55 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package drb;

import java.io.BufferedReader;
import java.sql.*;

/**
 *
 * @author Asger
 */
public class TicketCommandGroup extends PromptingCommand {
    
    private ViewTicketCommand viewTicketCmd = new ViewTicketCommand(input);
    private FindFreeConnectionsCommand findConnectionsCmd = new FindFreeConnectionsCommand(input);
    private IssueTicketCommand issueTicketCmd = new IssueTicketCommand(input, findConnectionsCmd);
    private Passenger passenger;
    
    /** Creates a new instance of TicketCommandGroup */
    public TicketCommandGroup(BufferedReader input) {
        super(input);
    }
    
    public void setConnection(Connection connection) {
        super.setConnection(connection);
        viewTicketCmd.setConnection(connection);
        findConnectionsCmd.setConnection(connection);
        issueTicketCmd.setConnection(connection);
    }
    
    public void printHelp() {
        System.out.println("-------------------------------");
        System.out.println("Ticket commands for "+passenger.name+":");
        System.out.println(" view     | Views unexpired tickets owned by you");
        System.out.println(" find     | Finds connections");
        System.out.println(" issue    | Issues a new ticket from a connection found with 'find'");
        System.out.println(" back     | Returns to the root menu");
        System.out.println(" help     | Show this help");
        System.out.println("-------------------------------");
    }
    
    public void execute() throws SQLException {       
        passenger = promptPassenger(true);
        
        viewTicketCmd.passenger = passenger;
        issueTicketCmd.passenger = passenger;
        
        printHelp();
        
        while (true) {
            
            try {
                String cmd = getInput();
                if (cmd.equals("view")) {
                    viewTicketCmd.execute();
                }
                else if (cmd.equals("find")) {
                    findConnectionsCmd.execute();
                }
                else if (cmd.equals("issue")) {
                    issueTicketCmd.execute();
                }
                else if (cmd.equals("back")) {
                    break;
                }
                else if (cmd.equals("help")) {
                    printHelp();
                }
                else {
                    System.err.println("Unknown command. Type 'help' for a list of commands.");
                }
            } catch (CancelCommandException ex) {
                printHelp();
                // A command was cancelled.
                // Just catch the exception and keep running from here.
            }
        }
        
    }
}















